package pers.lws.erp.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.lws.erp.common.emum.MsgStatusEmum;
import pers.lws.erp.common.result.ResultBody;
import pers.lws.erp.entity.SysUser;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuws
 * @since 2020-09-11
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @PostMapping("/login")
    public ResultBody login(@RequestBody SysUser user) {
        // 获取当前主体
        Subject subject = SecurityUtils.getSubject();
        // 创建令牌
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getPassword());
        // 认证
        subject.login(token);
        // 如果认证成功
        if (subject.isAuthenticated()) {
            return ResultBody.success(MsgStatusEmum.USER_LOGIN_SUCCESS.getMsg(), subject.getPrincipal());
        } else {
            return ResultBody.error(MsgStatusEmum.USER_LOGIN_ERROR.getMsg());
        }
    }
}

